package com.example.t1;

import android.content.ComponentName;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class GameFragment extends Fragment {

    Chessboard chessboard = new Chessboard();
    ImageView img[][] = new ImageView[9][8];
    View view;
    int[] scr_img = {R.mipmap.im0,R.mipmap.im1,R.mipmap.im2,R.mipmap.im3,R.mipmap.im4,R.mipmap.im5,R.mipmap.im6,R.mipmap.im7,R.mipmap.im8,R.mipmap.im9,
            R.mipmap.im10,R.mipmap.im11,R.mipmap.im12,R.mipmap.im13,R.mipmap.im14,R.mipmap.im15,R.mipmap.im16,R.mipmap.im17,R.mipmap.im18,R.mipmap.im19,
            R.mipmap.im20,R.mipmap.im21,R.mipmap.im22,R.mipmap.im23,R.mipmap.im24,R.mipmap.im25};

    ImageClickedListener mCallback;
    public interface ImageClickedListener{
        public void removeimg(ImageView img);
    }

    public GameFragment() {
        // Required empty public constructor
    }

    public void repaint(){
        for(int i=0;i<9;i++){
            for(int j=0;j<8;j++){
                if(chessboard.getNo(i+1,j+1)==-1){
                    img[i][j].setVisibility(View.INVISIBLE);
                    continue;
                }
                int no = chessboard.getNo(i+1,j+1);
                img[i][j].setVisibility(View.VISIBLE);
                img[i][j].setImageResource(scr_img[no]);
            }
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_game, container, false);
        TableLayout ll = (TableLayout) view.findViewById(R.id.fuqing);
        for(int i=0;i<9;i++){
            TableRow tr = (TableRow) ll.getChildAt(i);
            for(int j=0;j<8;j++){
                img[i][j] = (ImageView) tr.getChildAt(j);
            }
        }

        try{
            mCallback = (ImageClickedListener)getActivity();
        }
        catch (ClassCastException e){
            throw e;//new ClassCastException(getActivity().toString()+"must implement ImageClickedListener")
        }

        for(int i=0;i<9;i++){
            for(int j=0;j<8;j++){
                ImageView tinyImage = img[i][j];
                if(tinyImage!=null){
                    tinyImage.setId((i+1)*10+j+1);

                    tinyImage.setOnClickListener(new View.OnClickListener(){
                        @Override
                        public void onClick(View view) {
                            mCallback.removeimg((ImageView)view);
                            repaint();//chessboard.isWin()
                            if(chessboard.isWin()){
                                Toast.makeText(getContext(),"你赢了",Toast.LENGTH_SHORT).show();

                                Intent intent = new Intent();
                                intent.putExtra("timeNum",timeNum);
                                Log.i("time",timeNum+"");
                                intent.putExtra("name",Chessboard.name);
                                intent.putExtra("winMsg","happy");
                                intent.setAction("com.example.t1.BBB");
                                intent.setComponent(new ComponentName("com.example.t1","com.example.t1.MyReceiver"));
                                view.getContext().sendBroadcast(intent);

                                DatabaseHelper databaseHelper = new DatabaseHelper(view.getContext(),"demo.db",null,1);
                                SQLiteDatabase db = databaseHelper.getWritableDatabase();
                                String sql = "insert into rank(name,time,photo) values(?,?,?)";
                                Object[] args = new Object[3];
                                args[0] = Chessboard.name;
                                args[1] = timeNum;
                                args[2] = Chessboard.photo;
                                db.execSQL(sql,args);
                                handler.removeCallbacks(cntRun);
                            }
                        }
                    });
                }
            }
        }
        repaint();

        handler.postDelayed(cntRun,1000);
        timeShowView = view.findViewById(R.id.time_show);
        return view;
    }
    Handler handler = new Handler();
    TextView timeShowView = null;
    static int timeNum = 0;
    public Runnable cntRun = new Runnable(){
        @Override
        public void run() {
            timeShowView.setText("用时："+String.valueOf(++timeNum));
            handler.postDelayed(cntRun,1000);
        }
    };
}